import { contextBridge,ipcRenderer  } from 'electron'
import { electronAPI } from '@electron-toolkit/preload'

// Custom APIs for renderer
const api = {}

// Use `contextBridge` APIs to expose Electron APIs to
// renderer only if context isolation is enabled, otherwise
// just add to the DOM global.
if (process.contextIsolated) {
  console.log(1)

  try {
    contextBridge.exposeInMainWorld('electron', electronAPI)
    contextBridge.exposeInMainWorld('api', api)
    contextBridge.exposeInMainWorld('ipcRenderer',ipcRenderer)//通信
  } catch (error) {
    console.error(error)
  }
} else {
  window.electron = electronAPI
  window.api = api
  window.ipcRenderer = ipcRenderer
}

//自定义api 发送消息到主进程
contextBridge.exposeInMainWorld('send_main', {
  query_db : async (sql,callback) => {
   const result = await ipcRenderer.invoke('query_db',sql)
   callback(result)
  },
  upload_file_to_db: async (task_id,callback) => {
    const result = await ipcRenderer.invoke('upload_file_to_db',task_id)
    callback(result)
  },
  create_task : async (request_data,callback) => {
    const result = await ipcRenderer.invoke('create_task',request_data)
    callback(result)
  },
  exec_db : async (sql,callback) => {
    const result = await ipcRenderer.invoke('exec_db',sql)
    callback(result)
  },
});
